package com.ghl.springbootmysqlmasterslave.controller;

import com.ghl.springbootmysqlmasterslave.entity.User;
import com.ghl.springbootmysqlmasterslave.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-01-06 11:24:54
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public User findById(@PathVariable Integer id) {
        User user = userService.findById(id);
        return user;
    }

    /**
     * 新增
     *
     * @param user
     * @return
     */
    @PostMapping
    public User insert(User user) {
        User insert = userService.insert(user);
        return insert;
    }

    /**
     * 修改
     *
     * @param user
     * @return
     */
    @PutMapping
    public User update(User user) {
        User update = userService.update(user);
        return update;
    }

    /**
     * 删除
     *
     * @param id
     */
    @DeleteMapping("{id}")
    public void delete(@PathVariable int id) {
        userService.deleteById(id);
    }

    @GetMapping("list")
    public List<User> findAll() {
        return userService.findAll();
    }

}